using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Resources;
using ALV = AsyncListView.AsyncListView;
using GA;

namespace _queensGA
{
    public partial class frmMain : Form
    {
        private List<frmBoard> lstBoards;

        public frmMain()
        {
            InitializeComponent();
            lstBoards = new List<frmBoard>();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            int N = (int)upDownN.Value;

            frmBoard b = new frmBoard((int)upDownN.Value, (int)upDownP.Value, (double)upDownM.Value, (double)upDownC.Value, (double)upDownE.Value);

            alvThreads.AddRow(b.BoardId, string.Format("Board Size: {0}", N), ALV.IconKey.Waiting, true);
            b.UpdateState += new frmBoard.UpdateStateHandler(b_UpdateState);
            b.EvolutionPaused += new frmBoard.EvolutionPausedHandler(b_EvolutionPaused);
            b.EvolutionCanceled += new frmBoard.EvolutionCanceledHandler(b_EvolutionCanceled);
            lstBoards.Add(b);
            b.Start();
            alvThreads.UpdateRowState(b.BoardId, ALV.IconKey.Processing);
            b.Show();
        }

        void b_EvolutionCanceled(frmBoard board)
        {
            alvThreads.UpdateRowState(board.BoardId, ALV.IconKey.Failure, board.GetInfo());
        }

        void b_EvolutionPaused(frmBoard board, bool isPaused)
        {
            alvThreads.UpdateRowState(board.BoardId, isPaused ? ALV.IconKey.Waiting : ALV.IconKey.Processing, board.GetInfo());
        }

        void b_UpdateState(frmBoard board, Population p)
        {
            if (p.bestSoFar.Fitness != 0)
            {
                alvThreads.UpdateRowState(board.BoardId, ALV.IconKey.Processing, board.GetInfo());
            }
            else
            {
                alvThreads.UpdateRowState(board.BoardId, ALV.IconKey.Success, board.GetInfo(), p.bestSoFar);
            }
            alvThreads.UpdateProgress(board.BoardId, 100 - (int)(100.0 * p.bestSoFar.Fitness / (p.N * (p.N - 1))));
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (frmBoard b in lstBoards)
            {
                b.Close();
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            alvThreads.lvMain.Columns[1].Width = 100;
            alvThreads.lvMain.Columns[3].Width = 450;
            alvThreads.ListViewLinkClick += new AsyncListView.AsyncListView.LinkClickHandler(alvThreads_ListViewLinkClick);
        }

        void alvThreads_ListViewLinkClick(object tag)
        {
            if (tag != null)
            {
                frmSolution fs = new frmSolution((tag as Chromosome).genes);
                fs.Show();
            }
        }


    }
}